package org.lzy.shop.viewmodel.repository.lmpl

import android.content.Context
import android.net.Uri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.lzy.shop.datas.OrderCommentRequest
import org.lzy.shop.datas.OrderRequest
import org.lzy.shop.network.KtorApiService
import org.lzy.shop.response.AddressResponse
import org.lzy.shop.response.LogisticsResponse
import org.lzy.shop.response.OrderInfoResponse
import org.lzy.shop.response.OrderRefundReason
import org.lzy.shop.response.OrderResponse
import org.lzy.shop.response.OssUpload
import org.lzy.shop.viewmodel.repository.BaseRepository
import org.lzy.shop.viewmodel.repository.OrderRepository

class OrderRepositoryImpl : BaseRepository(),OrderRepository {


    override suspend fun getOrderList(orderPresent: OrderRequest): OrderResponse? {
        return safeApiCall {
            val response = KtorApiService.getOrder(orderPresent)
            handleResponse(response)
        }
    }

    override suspend fun cancelOrder(orderId: Int, remark: String?): String? {
        return safeApiCall {
            val response = KtorApiService.cancelOrder(orderId, remark)
            handleResponse(response)
        }
    }

    override suspend fun getOrderInfo(orderId: Int): OrderInfoResponse? {
        return safeApiCall {
            val response = KtorApiService.getOrderInfo(orderId)
            handleResponse(response)
        }
    }

    override suspend fun confirmOrder(orderId: Int): String? {
        return safeApiCall {
            val response = KtorApiService.confirmOrder(orderId)
            handleResponse(response)
        }
    }

    override suspend fun refundOrder(orderId: Int, reason: String?): Any? {
        return safeApiCall {
            val response = KtorApiService.refundOrder(orderId, reason)
            handleResponse(response)
        }
    }

    override suspend fun getLogistics(orderId: Int): LogisticsResponse? {
        return safeApiCall {
            val response = KtorApiService.getLogistics(orderId)
            handleResponse(response)
        }
    }

    override suspend fun getDefaultAddress(): AddressResponse? {
        return safeApiCall {
            val response = KtorApiService.getDefaultAddress()
            handleResponse(response)
        }
    }

    override suspend fun getReasonData(): MutableList<OrderRefundReason>? {
        return withContext(Dispatchers.IO) {
            val response = KtorApiService.getReasonData()
            if (!response.isFailed()) {
                response.data.orderRefundReason
            } else {
                null
            }
        }
    }

    override suspend fun getTencentCredentials(): OssUpload? {
        return safeApiCall {
            val response = KtorApiService.getUploadCredentials()
            handleResponse(response)
        }
    }

    override suspend fun uploadImage(context: Context, imageUri: Uri, uploadUrl: String, tmpSecretId: String, tmpSecretKey: String, sessionToken: String): String? {
        return safeApiCall {
            val response = KtorApiService.uploadImageToTencent(imageUri, uploadUrl, tmpSecretId, tmpSecretKey, sessionToken)
            handleResponse(response)
        }
    }

    override suspend fun uploadImages(context: Context, imageUris: List<Uri>, uploadUrl: String, tmpSecretId: String, tmpSecretKey: String, sessionToken: String): List<String>? {
        return safeApiCall {
            val response = KtorApiService.uploadImagesToTencent(imageUris, uploadUrl, tmpSecretId, tmpSecretKey, sessionToken)
            handleResponse(response)
        }
    }

    override suspend fun orderComment(request: OrderCommentRequest): Any? {
        return safeApiCall {
            val response = KtorApiService.orderComment(request)
            handleResponse(response)
        }
    }
}